package com.xunyin.enums;

import com.xunyin.utils.StringUtils;

/**
 * @author jthong
 * @Description:控制板通道（当前最大为24个通道）
 * @Version 1.0.0
 * @Date 2019/12/26 16:18
 */
public enum CtrDeviceChannelEnum {
    /***
     * 通道1-24  0代表全部
     */
    CHANNEL_0(0, "00"),
    CHANNEL_1(1, "01"),
    CHANNEL_2(2, "02"),
    CHANNEL_3(3, "03"),
    CHANNEL_4(4, "04"),
    CHANNEL_5(5, "05"),
    CHANNEL_6(6, "06"),
    CHANNEL_7(7, "07"),
    CHANNEL_8(8, "08"),
    CHANNEL_9(9, "09"),
    CHANNEL_10(10, "0A"),
    CHANNEL_11(11, "0B"),
    CHANNEL_12(12, "0C"),
    CHANNEL_13(13, "0D"),
    CHANNEL_14(14, "0E"),
    CHANNEL_15(15, "0F"),
    CHANNEL_16(16, "10"),
    CHANNEL_17(17, "11"),
    CHANNEL_18(18, "12"),
    CHANNEL_19(19, "13"),
    CHANNEL_20(20, "14"),
    CHANNEL_21(21, "15"),
    CHANNEL_22(22, "16"),
    CHANNEL_23(23, "17"),
    CHANNEL_24(24, "18");

    private final String name;
    private final int value;

    CtrDeviceChannelEnum(int value, String name) {
        this.name = name;
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return value;
    }

    public static CtrDeviceChannelEnum parse(Integer value) {
        if (value == null) {
            return throwParseException(value);
        }
        for (CtrDeviceChannelEnum e : CtrDeviceChannelEnum.values()) {
            if (e.getValue() == value) {
                return e;
            }
        }
        return throwParseException(value);
    }

    private static CtrDeviceChannelEnum throwParseException(Integer value) {
        throw new IllegalArgumentException(value + " can not parse to " + CtrDeviceChannelEnum.class.getName());
    }

    public static String getNameByValue(Integer value) {
        if (value == null) {
            return "";
        }
        for (CtrDeviceChannelEnum e : CtrDeviceChannelEnum.values()) {
            if (e.getValue() == value) {
                return e.getName();
            }
        }
        return "";
    }

    public static CtrDeviceChannelEnum getObjectByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        for (CtrDeviceChannelEnum e : CtrDeviceChannelEnum.values()) {
            if (e.getName().equals(name)) {
                return e;
            }
        }
        return null;
    }
}
